from construct2 import *


#===============================================================================
# Lambda: rely on python's lazy binding
#===============================================================================
c1 = Struct("foo",
    Byte("value"),
    Flag("has_next"),
    If(lambda ctx: ctx["has_next"],
        LazyBound("foo", lambda: c1)
    )
)
#c2 = Struct("foo",
#    Byte("value"),
#    Flag("has_next"),
#    If2(lambda ctx: ctx["has_next"],
#        LazyBound("foo", lambda: c2)
#    )
#)

import time
def timeit(c, count = 10000):
    t = time.time()
    while count:
        o = c.parse("A\x01" "B\x01" "C\x01" "D\x01" "E\x01" "E\x01" "E\x01" "E\x01" "E\x01" "E\x00")
        count -= 1
    return time.time() - t

print timeit(c1)
#b = timeit(c2)
#print a, b
#print "%.2f%%" % ((1 - (b / a)) * 100,)


#
#c = Struct("foo",
#    UBInt8("blah1"),
#    OnDemand(
#        String("data", 11)
#    ),
#    UBInt8("blah2"),
#)
#o = c.parse("Xhello worldY")
#print o
##print o.data.value
#print repr(c.build(o))

